package com.app.listen;

import com.alibaba.fastjson.JSON;
import com.app.common.RabbitConst;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author xiaoyu.fang
 * @date 2020/1/8 14:09
 */
@Slf4j
@Component
public class BizDelayListen {

    @RabbitHandler
    @RabbitListener(queues = RabbitConst.DELAY_DIRECT_QUEUE)
    public void taskDelayQueue(String msg, Message message, Channel channel) throws IOException {
        try {
            // 检测次数
            Map<String, Object> maps = (Map<String, Object>) JSON.parse(msg);
            String id = (String) maps.get("id");
            String info = (String) maps.get("msg");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            log.info("{} =============== 这是延迟队列：id = {}， msg = {}", format.format(new Date()), id, info);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
